package com.zhiyou100.blog.service;

import com.zhiyou100.blog.dao.TitleDao;
import com.zhiyou100.blog.dao.UserDao;
import com.zhiyou100.blog.dao.impl.MessageDaoImpl;
import com.zhiyou100.blog.dao.impl.TitleDaoImpl;
import com.zhiyou100.blog.dao.impl.UserDaoImpl;
import com.zhiyou100.blog.entity.PageBean;
import com.zhiyou100.blog.entity.Title;
import com.zhiyou100.blog.entity.User;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @author shkstart
 * @create 2021-10-22 19:19
 */
public class TitleService {

    //private UserDao userDao = new UserDaoImpl();
    private TitleDao titleDao = new TitleDaoImpl();


    public int updateTitleGuanZhu(int tid) {
        return titleDao.updateTitleGuanZhu(tid);
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @return
     */
    public PageBean<Title> getListTitlePaging(Integer pageNum) {
        if (pageNum < 1) {
            pageNum = 1;
        }
        //根据数据库查询总记录数
        long totalRecord = titleDao.getCountTitles();
        //安全校验
        if (totalRecord != 0) {
            //当前页//需要显示的条目数 //总记录数
            PageBean<Title> titlePage = new PageBean<Title>(pageNum, 3, (int) totalRecord);
            //获取 文本内容
            List<Title> titlePaging = titleDao.getListTitlePaging(titlePage.getStartIndex(), titlePage.getPageSize());
            titlePaging.forEach(e -> {
                e.setUser(new UserDaoImpl().getOneById(e.getUid()));
            });
            //设置分页 集合
            titlePage.setList(titlePaging);
            return titlePage;
        }
        return null;
    }

    /**
     * 获取热点话题
     *
     * @return
     */
    public List<Title> getHotTopicList() {
        List<Title> list = titleDao.getListOrderByMaxAttention();
        return list;
    }

    public Title getOneTitleWithMessage(Integer tid) {
        if (tid != null) {
            //获取当的标题信息
            Title title = titleDao.getOneTitleById(tid);
            //如果 根据 tid没有查到就不再去查数据库了
            if (title != null) {
                EndorseService endorseService = new EndorseService(); //赞虚集合
                //获取 当前用户的信息
                title.setUser(new UserDaoImpl().getOneById(title.getUid()));
                //获取 该标题的留言信息
                title.setMessages(new MessageDaoImpl().getListMessageWithTid(title.getTid()));
                if (title.getMessages() != null) {
                    //添加 留言的用户
                    title.getMessages().forEach(e -> {
                        e.setUser(new UserService().getUserAndZanXuByUid(e.getUid()));
                        // e.setUser(new UserDaoImpl().getOneById(e.getUid()));
                    });
                    //赞虚信息
                    title.getMessages().forEach(e -> {
                        //留言用户的点赞虚的信息
                        e.getUser().setListZan(endorseService.getListEndorseZanXu(e.getMid(), true));
                        e.getUser().setListXu(endorseService.getListEndorseZanXu(e.getMid(), false));
                    });
                }

                return title;
            }
        }
        return null;
    }

    @Test
    public void test1() {
//        System.out.println(getListTitlePaging(0));
//        System.out.println(getOneTitleWithMessage(1001));
        System.out.println(queryListTitlePagingByQustion(1, "一"));
    }

    /**
     * 添加话题
     *
     * @param title
     */
    public void appentTitle(Title title) {
        int i = titleDao.appentTitle(title);
    }

    /**
     * 分页条件 查询
     *
     * @param queryText
     * @return
     */
    public PageBean<Title> queryListTitlePagingByQustion(int pageNum, String queryText) {
        long totalRecord = titleDao.queryTitleQuestion(queryText);
        if (totalRecord != 0L) {
            PageBean<Title> titlePage = new PageBean<Title>(pageNum, 3, (int) totalRecord);
            List<Title> list = titleDao.queryListTitleQuestion(titlePage.getStartIndex(), queryText);//查询的所有

            list.forEach(e -> {
                e.setUser(new UserDaoImpl().getOneById(e.getUid()));
            });
            titlePage.setList(list);
            return titlePage;
        }

        //安全校验
        if (totalRecord != 0) {
            //当前页//需要显示的条目数 //总记录数
            PageBean<Title> titlePage = new PageBean<Title>(pageNum, 3, (int) totalRecord);
            //获取 文本内容
            List<Title> titlePaging = titleDao.getListTitlePaging(titlePage.getStartIndex(), titlePage.getPageSize());
            titlePaging.forEach(e -> {
                e.setUser(new UserDaoImpl().getOneById(e.getUid()));
            });
            //设置分页 集合
            titlePage.setList(titlePaging);
            return titlePage;
        }

        return null;
    }


    public PageBean<Title> queryLIstTitlePagingByUid(int pageNum, int uid) {
        //查询总记录
        int totalRecord = (int) titleDao.getCountTitlesByUid(uid);
        //创建分页对象
        PageBean<Title> pageBean = new PageBean<>(pageNum, 2, totalRecord);
        //获取分页数据
        List list = titleDao.getListTitlePagingByUid(pageBean.getStartIndex(), uid);
        pageBean.setList(list);

        return pageBean;
    }
}
